﻿using System;
using System.Collections.Generic;
using System.Text;
using VisionCloud.Core.Model.Fms;
using VisionCloud.Domain.DtoModel.Fms;
using VisionCloud.Domain.Repository.Interface.Fms;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using Blm.Utils.Extensions;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.DtoModel.Ms;
using Microsoft.EntityFrameworkCore;
using VisionCloud.Domain.DtoModel.Inner;

namespace VisionCloud.Domain.Repository.Implement.Fms
{
    public class FmsProjectBuildingFloorRepository : BaseRepository<FmsProjectBuildingFloor>, IFmsProjectBuildingFloorRepository
    {
        public async Task<ProjectBuildingFloorNameDto> GetBuildingFloorName(BimMapCodeDto dto)
        {
            ProjectBuildingFloorNameDto result = null;

            var project = await Db.FmsProjectBuildingFloor.FirstOrDefaultAsync(p => p.ProjectCode == dto.ProjectCode && !p.IsDeleted);
            if (project == null)
                return result;

            List<Building> buildList = project.BuildingList.ToObject<List<Building>>();
            var build = buildList.FirstOrDefault(P => P.BuildingCode == dto.BuildingCode);
            if (build == null)
                return result;

            var floor = build.FloorList.FirstOrDefault(p => p.FloorCode == dto.FloorCode);
            if (floor == null)
                return result;

            result = new ProjectBuildingFloorNameDto
            {
                ProjectCode = dto.ProjectCode,
                ProjectName = project.ProjectName,
                BuildingCode = dto.BuildingCode,
                BuildingName = build.BuildingName,
                FloorCode = dto.FloorCode,
                FloorName = floor.FloorName
            };

            return result;
        }

        public async Task<ApiPageResult> GetList(ProjectBuildlingFloorSearchDto dto)
        {
            // 公司Id
            var companyGuid = OperatorProvider.Get().CompanyGuid;
            // 注意内部公司和外部公司的权限
            var sysOrg = Db.SysOrg.FirstOrDefault(p => p.Id == companyGuid && !p.IsDeleted);
            bool isInterOrg = sysOrg?.IsInterOrg ?? false;
            // 非内部公司不提供查询结果
            if (!isInterOrg && companyGuid != Guid.Empty)
            {
                return dto.CopyTo<ApiPageResult>();
            }

            dto.AscDesc = "asc";
            dto.OrderBy = "ProjectCode asc";
            var queryAble = from project in Db.FmsProjectBuildingFloor
                            where !project.IsDeleted && (string.IsNullOrEmpty(dto.Query) || project.ProjectName.Contains(dto.Query) || project.ProjectCode.Contains(dto.Query))
                            select project;

            var result = dto.CopyTo<ApiPageResult>();
            var projectList = await queryAble.ToPageList(dto);
            List<FmsProjectBuildingFloorListDto> list = new List<FmsProjectBuildingFloorListDto>();
            foreach (var item in projectList)
            {
                list.Add(new FmsProjectBuildingFloorListDto
                {
                    ProjectCode = item.ProjectCode,
                    ProjectName = item.ProjectName,
                    BuildingList = item.BuildingList.ToObject<List<FmsBuildingListDto>>()
                });
            }

            result.PageData = list;
            result.Total = dto.Total;

            return result;
        }
    }
}
